/**
 * 数组的深拷贝函数, 考虑循环引用
 * @param {Array} src
 * @param {Array} target
 */
export const cloneArr = (src, target) => {
  for (let item of src) {
    if (Array.isArray(item)) {
      target.push(cloneArr(item, []));
    } else if (typeof item === "object") {
      target.push(deepClone(item, {}));
    } else {
      target.push(item);
    }
  }
  return target;
}

/**
 * 对象的深拷贝实现, 考虑循环引用
 * @param {Object} src
 * @param {Object} target
 * @return {Object}
 */
export const deepClone = (src, target) => {
  const keys = Reflect.ownKeys(src);
  let value = null;
  for (let key of keys) {
    value = src[key];
    if (Array.isArray(value)) {
      target[key] = cloneArr(value, []);
    } else if (typeof value === "object") {
      // 如果是对象而且不是数组, 那么递归调用深拷贝
      target[key] = deepClone(value, {});
    } else {
      target[key] = value;
    }
  }

  return target;
}
/**
 * 将以base64的图片url数据转换为Blob
 * @param {String} urlData 用url方式表示的base64图片数据
 */
export const dataUrlToBlob = (urlData) => {
  var arr = urlData.split(','),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], {
    type: mime
  });
}
/**
 * Blob转成File, 模拟File对象, (File对象是一个特殊的Blob对象)
 * @param {Blob} blob 
 * @param {String} fileName 
 */
export const blobToFile = (blob, fileName) => {
  blob.lastModifiedDate = new Date();
  blob.name = fileName;
  return blob;
}